<template>
    <div>
        <bread-list :list="['Home','System',{name:'Link'},'Edit']"></bread-list>
        <a-card>
            <h2>{{editStatus == 0 ? '添加友链' : '修改友链'}}</h2>
			<a-form-model ref="ruleForm" :model="editForm">
				<a-form-model-item has-feedback label="友链标题" prop="linkName">
					<a-input  v-model="editForm.linkName" type="text" />
				</a-form-model-item>
				<a-form-model-item has-feedback label="友链地址" prop="linkUrl">
					<a-input  v-model="editForm.linkUrl" type="text" />
				</a-form-model-item> 
				<a-form-model-item v-if="editForm.createTime != undefined" has-feedback label="创建时间" prop="createTime">
					<a-input v-model="editForm.createTime" disabled type="text" />
				</a-form-model-item>
				<a-form-model-item v-if="editForm.updateTime != undefined" has-feedback label="上次修改时间" prop="updateTime">
					<a-input v-model="editForm.updateTime" disabled type="text" />
				</a-form-model-item>
                <a-form-model-item v-if="editForm.version != undefined" has-feedback label="版本" prop="version">
					<a-input v-model="editForm.version" disabled type="text" />
				</a-form-model-item>
				<a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
					<a-button type="primary" @click="submitForm('ruleForm')">
						Submit
					</a-button>
					<a-button style="margin-left: 10px" @click="resetForm('ruleForm')">
						Reset
					</a-button>
				</a-form-model-item>
			</a-form-model>
        </a-card>
    </div>
</template>

<script>
// addLink 20006 updateLink 20008
import {getLinkById, addLink, updateLink} from '@/api/System.js'
export default {
    name: 'LinkEdit',
    data () {
        return {
            editStatus: 0,
            editForm: {}
        }
    },
    created () {
        const id = this.$route.query.id
        if (id != undefined) {
            this.editStatus = 1
            this.getLink(id)
        }
    },
    methods: {
        async getLink(id) {
            const {data : res} = await getLinkById(id)
            if (res.code !== 20005) 
                return this.$message.error('加载失败')
            this.editForm = res.data
            // console.log(this.editForm)
            this.$message.success(res.msg)
        },
        submitForm(formName) {
            this.$refs[formName].validate(async valid => {
            if (valid) {
                // 添加时间轴
                if (this.editStatus == 0) {
                    const {data : res} = await addLink({
                        linkName: this.editForm.linkName,
                        linkUrl: this.editForm.linkUrl
                    })
                    if (res.code !== 20006) 
                        return this.$message.error('添加出错')
                    this.$message.success(res.msg)
                }
                // 修改时间轴
                if (this.editStatus == 1) {
                    console.log(this.editForm)
                    const {data : res} = await updateLink(this.editForm)
                    if (res.code !== 20008) 
                        return this.$message.error(res.msg)
                    this.$message.success('更新成功')
                }
                return this.$router.push('/system/link')
            }
        });
    },
    resetForm(formName) {
        this.$refs[formName].resetFields();
    },
    }
}
</script>

<style>

</style>